package model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import db.ConnectionManager;
import forms.FlightForm;

/**
 * 
 * @author Ceren BOZKURT
 * @author Duygu AYAN
 * @author Pelin OZTURK
 */

public class FlightContainer {

	public FlightForm searchFlight(Flight flight) {
		FlightForm flightForm = new FlightForm();
		Vector<Vector<Object>> flightList = new Vector<Vector<Object>>();
		Vector<String> flightMetadata = new Vector<String>();
		ConnectionManager mgr = ConnectionManager.getInstance();
		Connection con = mgr.getConnection();

		try {
			Statement st = con.createStatement() ;
			String query = "SELECT * FROM FLIGHT WHERE DEPT_POINT='" + flight.getDeparture().trim() + "' AND ARRIVAL_POINT='" +flight.getArrival().trim() + "' AND DEPT_DATE='"+ flight.getDepartureDate()+"'";
			System.out.println(query);
			ResultSet rs = st.executeQuery(query);
			ResultSetMetaData rsmd = rs.getMetaData();
			int columnCount = rsmd.getColumnCount();
			for(int i=0;i<columnCount;i++)
				flightMetadata.add(rsmd.getColumnName(i+1));

			flightForm.setFlightMetadata(flightMetadata);

			while (rs.next()) {
				Vector<Object> fVector = new Vector<Object>();
				fVector.add(rs.getString(1));
				fVector.add(rs.getString(2));
				fVector.add(rs.getString(3));
				fVector.add(rs.getString(4));
				fVector.add(rs.getString(5));
				fVector.add(rs.getString(6));
				fVector.add(rs.getString(7));
				fVector.add(rs.getInt(8));
				fVector.add(rs.getInt(9));

				System.out.println(fVector.get(0)+" "+ fVector.get(1) +" "+ fVector.get(2) +" "+ fVector.get(3)+" "+ fVector.get(4)+" "+ fVector.get(5)+" "+ fVector.get(6)+" "+ fVector.get(7)+" "+ fVector.get(8));

				flightList.add(fVector);
			}

			flightForm.setFlightList(flightList);

			rs.close();
			st.close();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			mgr.closeConnection(con);
		}

		// Return object is flightForm which held flightList and resultsetmetadata.
		return flightForm;
	}

}
